import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Parameter;

public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //获取class字节码文件对象
        Class c = Class.forName("Student");
        //获取构造方法(仅用public修饰的)
        Constructor[] constructors1 = c.getConstructors();
        for (Constructor constructor : constructors1) {
            System.out.println(constructor);
        }
        System.out.println("==========================================");

        //获取构造方法(所有修饰符修饰的)
        Constructor[] constructors2 = c.getDeclaredConstructors();
        for (Constructor constructor : constructors2) {
            System.out.println(constructor);
        }
        //获取单个构造方法(所有修饰符访问)
        System.out.println("==================================");

        Constructor c3 = c.getDeclaredConstructor(String.class);
        System.out.println(c3);

        //获取单个构造方法(public修饰符访问)
        System.out.println("==================================");

        Constructor c4 = c.getConstructor();
        System.out.println(c4);

        //获取构造方法的权限修饰符（修饰符以整数的形式存在）
        System.out.println("==================================");
        int modifiers = c3.getModifiers();
        System.out.println(modifiers);

        //获取到构造方法的参数
        System.out.println("===================");
        Parameter[] parameters = c3.getParameters();
        for (Parameter parameter : parameters) {
            System.out.println(parameter);
        }

        //利用私有的构造方法创建对象
        c3.setAccessible(true);//暴力反射:表示临时取消权限校验
        Student stu =(Student) c3.newInstance("爱莉西雅");
        System.out.println(stu);

    }


}
